import React, { useEffect, useState } from 'react';
import { Form, Input, Modal, Button, Row, Col, message } from 'antd';
import request from '@/http';
import { history } from '@/.umi/core/history';
export interface PasswordEditObject {
  isModalVisible: boolean;
  old_password?: String;
}

export default function passwordEdit(props: {
  editObject: PasswordEditObject;
  Pagejump: any;
}) {
  const [formObject] = Form.useForm(); //获取数据
  const { editObject, Pagejump } = props;
  const [isModalVisible, setIsModalVisible] = useState(false);
  const handleCancel = () => {
    setIsModalVisible(false);
  };
  const onFinish = async (values: any) => {
    await request
      .post(
        '/api/common_module/a_t_s/updatePassword',
        { ...values, oldPwd: editObject.old_password },
        { headers: { 'Content-Type': 'application/json' } },
      )
      .then((response: any) => {
        if (response.code == 200) {
          setIsModalVisible(false);
          Pagejump();
          message.success('密码修改成功！,请重新登录！！');
        } else {
          formObject.resetFields();
          message.error(response.message);
        }
      });
  };
  useEffect(() => {
    setIsModalVisible(editObject.isModalVisible);
  }, [editObject]);
  return (
    <>
      <Modal
        title="修改密码"
        visible={isModalVisible}
        footer={false}
        onCancel={handleCancel}
      >
        <Form onFinish={onFinish} form={formObject}>
          <Row justify="center">
            <Col span={22}>
              <Form.Item
                name="newPwd"
                label="新密码"
                rules={[
                  {
                    required: true,
                    message: '不能为空!',
                  },
                  {
                    pattern:
                      /^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)])+$).{6,16}$/,
                    message:
                      '只能是6~16位的字符,特殊字符只包括~@#$%*_-+=:,.?[]{}',
                  },
                ]}
                hasFeedback
              >
                <Input.Password />
              </Form.Item>
            </Col>
            <Col>
              <Form.Item
                name="conPwd"
                label="请再次输入新密码"
                dependencies={['newPwd']}
                hasFeedback
                rules={[
                  {
                    required: true,
                    message: '不能为空!',
                  },
                  ({ getFieldValue }) => ({
                    validator(_, value) {
                      if (!value || getFieldValue('newPwd') === value) {
                        return Promise.resolve();
                      }
                      return Promise.reject(new Error('两次新密码不一致!'));
                    },
                  }),
                ]}
              >
                <Input.Password />
              </Form.Item>
            </Col>
            <Col>
              <Button
                type="primary"
                htmlType="submit"
                style={{ marginRight: 10 }}
              >
                确认
              </Button>
              <Button type="primary" onClick={handleCancel}>
                取消
              </Button>
            </Col>
          </Row>
        </Form>
      </Modal>
    </>
  );
}
